from rest_framework.response import Response
from rest_framework.views import APIView

from apps.works.models import WorksList, Work
from apps.works.serializers import WorksListSerializer, WorkSerializer


class WorksListView(APIView):
    """作品列表页"""

    def get(self, request, *args, **kwargs):
        id = self.kwargs['pk']
        works = WorksList.objects.filter(category=id)
        serializer = WorksListSerializer(works, many=True)
        works_list = serializer.data
        response_data = {
            'works': works_list,
        }
        print(response_data)
        return Response(response_data)


class WorksDetailsView(APIView):
    """作品集详情页"""

    def get(self, request, *args, **kwargs):
        pk = self.kwargs['pk']
        try:
            works = WorksList.objects.get(id=pk)
            serializer_work = WorksListSerializer(works)
            works = serializer_work.data
        except:
            return Response()
        works_list = Work.objects.filter(works=pk)
        serializer = WorkSerializer(works_list, many=True)
        works_list = serializer.data
        count = Work.objects.filter(works=pk).count()

        response_data = {
            'performance': works_list,
            'count': count,
            'works': works,

        }
        # performance: [{
        #     name: "《人在江湖》郭德纲 于谦",
        #     year: "2年前",
        #     count: "5601.9万",
        #     time: "34:36"
        # },
        #     {
        #         name: "《人在江湖》郭德纲 于谦",
        #         year: "2年前",
        #         count: "5601.9万",
        #         time: "34:36"
        #     },

        # ]
        return Response(response_data)


class WorksTracksView(APIView):
    """作品集点击数"""

    def put(self, request, *args, **kwargs):
        pk = self.kwargs['pk']
        try:
            works = WorksList.objects.get(id=pk)
            works.tracks += 1
            works.save()
        except:
            return Response()
        serializer_work = WorksListSerializer(works)
        works = serializer_work.data

        response_data = {
            'works': works,

        }
        return Response(response_data)
